﻿
using System;
using System.Collections.Specialized;
using System.IO;
using System.Windows.Forms;
using GDFlacTool.Common;
using GDFlacTool.Properties;

namespace GDFlacTool.Forms
{
    public partial class FormMaskEditor : Form
    {
        private string _mask;
        private int _selectedFormatIndex;
        private readonly StringCollection _savedMasks = new StringCollection();
        private readonly string _currentPath;
        private readonly RenameType _renameType;
        private readonly string _currentExtension = string.Empty;

        public FormMaskEditor(string mask, string currentPath, RenameType renameType, string extension)
        {
            InitializeComponent();

            _mask = mask;

            _currentPath = currentPath;

            _renameType = renameType;

            _currentExtension = extension;

            txtMask.Text = Mask;

            if(_renameType == RenameType.FILENAME)
            {
                _savedMasks = MaskEditorSettings.Default.SavedRenameMasks;
            }
            else if(_renameType == RenameType.TAGFROMFILE)
            {
                _savedMasks = MaskEditorSettings.Default.SavedTagsFromFileMasks;
                btnFileName.Enabled = false;
                btnFileNumber.Enabled = false;
                btnBitrate.Enabled = false;
                btnPlayTime.Enabled = false;
                btnQuality.Enabled = false;
                btnEncoder.Enabled = false;
                btnLabel.Enabled = false;
                btnRemixedBy.Enabled = false;
                btnMood.Enabled = false;
                btnConductor.Enabled = false;
                btnDisc2.Enabled = false;
                btnTrack2.Enabled = false;
                btnBpm.Enabled = false;
                btnComposer.Enabled = false;
                btnInsertFolder.Enabled = false;
            }
            else if(_renameType == RenameType.FOLDERRENAME)
            {
                _savedMasks = MaskEditorSettings.Default.SavedFolderRenameMasks;

                btnFileName.Enabled = false;
                btnFileNumber.Enabled = false;
                btnTitle.Enabled = false;
                btnTrack.Enabled = false;
                btnComment.Enabled = false;
                btnCatalog.Enabled = false;
                btnPlayTime.Enabled = false;
                btnQuality.Enabled = false;
                btnEncoder.Enabled = false;
                btnLabel.Enabled = false;
                btnRemixedBy.Enabled = false;
                btnMood.Enabled = false;
                btnDisc2.Enabled = false;
                btnTrack2.Enabled = false;
                btnBpm.Enabled = false;
                btnInsertFolder.Enabled = false;
            }
        }

        #region Properties.

        public string Mask
        {
            get { return _mask; }
        }

        #endregion

        #region Form Events.

        private void FormMaskEditor_Load(object sender, EventArgs e)
        {
            LoadSavedMasks();
        }

        private void FormMaskEditor_FormClosing(object sender, FormClosingEventArgs e)
        {
            if(_renameType == RenameType.FILENAME)
            {
                MaskEditorSettings.Default.SavedRenameMasks = _savedMasks;
            }
            else if (_renameType == RenameType.TAGFROMFILE)
            {
                MaskEditorSettings.Default.SavedTagsFromFileMasks = _savedMasks;
            }
            else if(_renameType == RenameType.FOLDERRENAME)
            {
                MaskEditorSettings.Default.SavedFolderRenameMasks = _savedMasks;
            }

            MaskEditorSettings.Default.Save();
        }

        #endregion

        #region Private Methods.

        private string BuildFormatString(int index, string currentFormat, string addedFormat)
        {
            string retvalue = currentFormat;

            retvalue = retvalue.Insert(index, addedFormat);

            return retvalue;
        }

        private void LoadSavedMasks()
        {
            foreach (string mask in _savedMasks)
            {
                lsbSavedMasks.Items.Add(mask);
            }
        }

        private void SetCursor()
        {
            _selectedFormatIndex = txtMask.Text.Length;
            txtMask.Select();
            txtMask.SelectionStart = _selectedFormatIndex;
        }

        private string ShowPreView(string mask)
        {
            if (string.IsNullOrEmpty(mask))
                return string.Empty;

            string result = mask;

            MaskHelperCollection helper = new MaskHelperCollection();

            foreach (MaskHelper maskHelper in helper)
            {
                result = result.Replace(maskHelper.Mask, maskHelper.Example);
            }

            return result;
        }

        #endregion

        #region Mask Buttons Events.

        private void btnArtist_Click(object sender, EventArgs e)
        {
            txtMask.Text = BuildFormatString(_selectedFormatIndex, txtMask.Text, btnArtist.Tag.ToString());
            SetCursor();
        }

        private void btnTitle_Click(object sender, EventArgs e)
        {
            txtMask.Text = BuildFormatString(_selectedFormatIndex, txtMask.Text, btnTitle.Tag.ToString());
            SetCursor();
        }

        private void btnAlbum_Click(object sender, EventArgs e)
        {
            txtMask.Text = BuildFormatString(_selectedFormatIndex, txtMask.Text, btnAlbum.Tag.ToString());
            SetCursor();
        }

        private void btnYear_Click(object sender, EventArgs e)
        {
            txtMask.Text = BuildFormatString(_selectedFormatIndex, txtMask.Text, btnYear.Tag.ToString());
            SetCursor();
        }

        private void btnGenre_Click(object sender, EventArgs e)
        {
            txtMask.Text = BuildFormatString(_selectedFormatIndex, txtMask.Text, btnGenre.Tag.ToString());
            SetCursor();
        }

        private void btnTrack_Click(object sender, EventArgs e)
        {
            txtMask.Text = BuildFormatString(_selectedFormatIndex, txtMask.Text, btnTrack.Tag.ToString());
            SetCursor();
        }

        private void btnTrack2_Click(object sender, EventArgs e)
        {
            txtMask.Text = BuildFormatString(_selectedFormatIndex, txtMask.Text, btnTrack2.Tag.ToString());
            SetCursor();
        }

        private void btnFileName_Click(object sender, EventArgs e)
        {
            txtMask.Text = BuildFormatString(_selectedFormatIndex, txtMask.Text, btnFileName.Tag.ToString());
            SetCursor();
        }

        private void btnComment_Click(object sender, EventArgs e)
        {
            txtMask.Text = BuildFormatString(_selectedFormatIndex, txtMask.Text, btnComment.Tag.ToString());
            SetCursor();
        }

        private void btnFileNumber_Click(object sender, EventArgs e)
        {
            txtMask.Text = BuildFormatString(_selectedFormatIndex, txtMask.Text, btnFileNumber.Tag.ToString());
            SetCursor();
        }

        private void btnBitrate_Click(object sender, EventArgs e)
        {
            txtMask.Text = BuildFormatString(_selectedFormatIndex, txtMask.Text, btnBitrate.Tag.ToString());
            SetCursor();
        }

        private void btnPlayTime_Click(object sender, EventArgs e)
        {
            txtMask.Text = BuildFormatString(_selectedFormatIndex, txtMask.Text, btnPlayTime.Tag.ToString());
            SetCursor();
        }

        private void btnBpm_Click(object sender, EventArgs e)
        {
            txtMask.Text = BuildFormatString(_selectedFormatIndex, txtMask.Text, btnBpm.Tag.ToString());
            SetCursor();
        }

        private void btnComposer_Click(object sender, EventArgs e)
        {
            txtMask.Text = BuildFormatString(_selectedFormatIndex, txtMask.Text, btnComposer.Tag.ToString());
            SetCursor();
        }

        private void btnAlbumArtist_Click(object sender, EventArgs e)
        {
            txtMask.Text = BuildFormatString(_selectedFormatIndex, txtMask.Text, btnAlbumArtist.Tag.ToString());
            SetCursor();
        }

        private void btnConductor_Click(object sender, EventArgs e)
        {
            txtMask.Text = BuildFormatString(_selectedFormatIndex, txtMask.Text, btnConductor.Tag.ToString());
            SetCursor();
        }

        private void btnDisc_Click(object sender, EventArgs e)
        {
            txtMask.Text = BuildFormatString(_selectedFormatIndex, txtMask.Text, btnDisc.Tag.ToString());
            SetCursor();
        }

        private void btnDisc2_Click(object sender, EventArgs e)
        {
            txtMask.Text = BuildFormatString(_selectedFormatIndex, txtMask.Text, btnDisc2.Tag.ToString());
            SetCursor();
        }

        private void btnCatalog_Click(object sender, EventArgs e)
        {
            txtMask.Text = BuildFormatString(_selectedFormatIndex, txtMask.Text, btnCatalog.Tag.ToString());
            SetCursor();
        }

        private void brnMood_Click(object sender, EventArgs e)
        {
            txtMask.Text = BuildFormatString(_selectedFormatIndex, txtMask.Text, btnMood.Tag.ToString());
            SetCursor();
        }

        private void btnLabel_Click(object sender, EventArgs e)
        {
            txtMask.Text = BuildFormatString(_selectedFormatIndex, txtMask.Text, btnLabel.Tag.ToString());
            SetCursor();
        }

        private void btnRemixedBy_Click(object sender, EventArgs e)
        {
            txtMask.Text = BuildFormatString(_selectedFormatIndex, txtMask.Text, btnRemixedBy.Tag.ToString());
            SetCursor();
        }

        private void btnQuality_Click(object sender, EventArgs e)
        {
            txtMask.Text = BuildFormatString(_selectedFormatIndex, txtMask.Text, btnQuality.Tag.ToString());
            SetCursor();
        }

        private void btnEncoder_Click(object sender, EventArgs e)
        {
            txtMask.Text = BuildFormatString(_selectedFormatIndex, txtMask.Text, btnEncoder.Tag.ToString());
            SetCursor();
        }

        #endregion

        #region txtMask Events.

        private void txtMask_Click(object sender, EventArgs e)
        {
            _selectedFormatIndex = txtMask.SelectionStart;
        }

        private void txtMask_TextChanged(object sender, EventArgs e)
        {
            _selectedFormatIndex = txtMask.SelectionStart;

            txtResultExample.Text = string.Format("{0}{1}", ShowPreView(txtMask.Text), _currentExtension);
        }

        private void txtMask_KeyUp(object sender, KeyEventArgs e)
        {
            _selectedFormatIndex = txtMask.SelectionStart;
        }

        #endregion

        #region ListBox Events.

        private void lsbSavedMasks_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            if(lsbSavedMasks.SelectedItem != null)
            {
                txtMask.Text = lsbSavedMasks.SelectedItem.ToString();
            }
        }

        #endregion

        #region Button Events.

        private void btnAddCurrentMask_Click(object sender, EventArgs e)
        {
            if(!lsbSavedMasks.Items.Contains(txtMask.Text) && !string.IsNullOrEmpty(txtMask.Text))
            {
                lsbSavedMasks.Items.Clear();

                _savedMasks.Add(txtMask.Text);

                LoadSavedMasks();
            }
        }

        private void btnRemoveSavedMask_Click(object sender, EventArgs e)
        {
            if(lsbSavedMasks.SelectedItem != null)
            {
                _savedMasks.Remove(lsbSavedMasks.SelectedItem.ToString());
                lsbSavedMasks.Items.Remove(lsbSavedMasks.SelectedItem);
                
            }
        }

        private void btnInsertFolder_Click(object sender, EventArgs e)
        {
            folderBrowserDialog.Description = "Select a folder for the renamed file";
            folderBrowserDialog.SelectedPath = _currentPath;

            DialogResult result = folderBrowserDialog.ShowDialog(this);

            if (result == DialogResult.OK)
            {
                string path = string.Format("{0}{1}", folderBrowserDialog.SelectedPath, Path.DirectorySeparatorChar);

                txtMask.Text = txtMask.Text.Insert(0, path);

                SetCursor();
            }
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            _mask = txtMask.Text;
        }

        #endregion
    }
}
